package com.hcb.tonguetiphealth.widget.homepage;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout;

import com.hcb.tonguetiphealth.adapter.OrderedExpandableListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.Ordered;
import com.hcb.tonguetiphealth.model.OrderedGroup;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class HomepageOrderedWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "HomepageOrderedWidget";
	private String url = Global.URL + Global.servlet_ordered;
	
	private Activity ctx;
	private Handler mHandler;
	private LinearLayout ll_back;
	private OrderedExpandableListAdapter orderedExpandableListAdapter;
	private List<OrderedGroup> groupList;
	private ExpandableListView expandableList;
	private OrderedGroup group;
	private List<Ordered> orderedList;
	
	public HomepageOrderedWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.homepage_personal_ordered, this); 
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取订单列表...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					expandableList.setAdapter(orderedExpandableListAdapter);
					dismissMessageDialog();
					break;
				}
			}
		};
		ll_back = (LinearLayout)findViewById(R.id.homepage_ordered_back_button);
		ll_back.setOnClickListener(this);
		expandableList = (ExpandableListView)findViewById(R.id.homepage_ordered_expandable);
		
		initData();
		
		expandableList.setGroupIndicator(null);
		expandableList.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				Ordered ordered = groupList.get(groupPosition).getChildList().get(childPosition);
				ctx.getIntent().putExtra("type", (groupPosition + 1));
				ctx.getIntent().putExtra("ordered", ordered);
				ctx.setContentView(new HomepageOrderedInfoWidget(ctx));
				return false;
			}
		});
		
	}
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.homepage_ordered_back_button:
			ctx.setContentView(new HomepagePersonalWidget(ctx));
			break;
		default:
			break;
		}
	}
	
	private void initData() {
		groupList = new ArrayList<OrderedGroup>();
		orderedExpandableListAdapter = new OrderedExpandableListAdapter(ctx, groupList);
		
		orderedList = new ArrayList<Ordered>();
		
		downOrdereds();
	}
	
	private void downOrdereds() {
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		new Thread() {
			public void run() {
				orderedList.clear();
				String response = null;
				try {
					List <NameValuePair> params = new ArrayList <NameValuePair>();
					params.add(new BasicNameValuePair("userId", Global.user.getUserId()));
					response = HttpUtils.Post(url, params);
					
					if(Global.DEBUG) {
						System.out.println("downRes -----------------------------\n" + response);
					}
				} catch(Exception e) {
					Log.e(TAG, "Exception", e);
				}
				if(!StringUtils.isEmpty(response)) {
					JSONManager jManager = new JSONManager();
					boolean isGotJSONObject = jManager.isGotJSONObject2(response);
					if(isGotJSONObject) {
						JSONObject jObject = jManager.getJsonObject();
						JSONArray jWineList = getJSONArray(jObject, "wine");
						JSONArray jMealList = getJSONArray(jObject, "meal");
						JSONArray jCateList = getJSONArray(jObject, "cate");
						JSONObject orderedObject;
						Ordered ordered;
						if(null == jWineList) {
							
						} else {
							group = new OrderedGroup();
							group.setGroupName("酒水订单");
							orderedList = new ArrayList<Ordered>();
							for(int i=0; i< jWineList.length(); i++) {
								orderedObject = getJSONObject(jWineList, i);
								if(null != orderedObject) {
									ordered = new Ordered();
									ordered.setId(getString(orderedObject, "itemid"));
									ordered.setTime(getString(orderedObject, "addtime"));
									ordered.setRemark(getString(orderedObject, "note"));
									ordered.setPrice(getString(orderedObject, "amount"));
									ordered.setType(getString(orderedObject, "type"));

									orderedList.add(ordered);
								}
							}
							group.setChildList(orderedList);
							groupList.add(group);
						}
						if(null == jMealList) {
							
						} else {
							group = new OrderedGroup();
							group.setGroupName("美食订单");
							orderedList = new ArrayList<Ordered>();
							for(int i=0; i< jMealList.length(); i++) {
								orderedObject = getJSONObject(jMealList, i);
								if(null != orderedObject) {
									ordered = new Ordered();
									ordered.setId(getString(orderedObject, "itemid"));
									ordered.setTime(getString(orderedObject, "addtime"));
									ordered.setRemark(getString(orderedObject, "note"));
									ordered.setPrice(getString(orderedObject, "amount"));
									ordered.setType(getString(orderedObject, "type"));

									orderedList.add(ordered);
								}
							}
							group.setChildList(orderedList);
							groupList.add(group);
						}
						if(null == jCateList) {
							
						} else {
							group = new OrderedGroup();
							group.setGroupName("套餐订单");
							orderedList = new ArrayList<Ordered>();
							for(int i=0; i< jCateList.length(); i++) {
								orderedObject = getJSONObject(jCateList, i);
								if(null != orderedObject) {
									ordered = new Ordered();
									ordered.setId(getString(orderedObject, "itemid"));
									ordered.setTime(getString(orderedObject, "addtime"));
									ordered.setRemark(getString(orderedObject, "note"));
									ordered.setPrice(getString(orderedObject, "amount"));
									ordered.setType(getString(orderedObject, "type"));

									orderedList.add(ordered);
								}
							}
							group.setChildList(orderedList);
							groupList.add(group);
						}
						int count = jManager.getResult();
						String msg = jManager.getMsg();
						System.out.println("------ 得到msg: " + count + ":" + msg);
					}
				}
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			}
		}.start();
	}
	
	@Override
	public void notifyAdapter() {
		
	}
}
